package com.ruoyi.business.controller;

import com.ruoyi.bizsys.domain.IncreasedLimitList;
import com.ruoyi.business.service.ICreditAccountInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ObjectUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 授信额度控制类
 *
 * @Author:
 * @Date: 2021/7/5 10:37 上午
 */
@RestController
@RequestMapping("/manual/credit")
public class CreditLimitController extends BaseController {

    @Autowired
    private ICreditAccountInfoService creditAccountInfoService;

    /**
     * 导入提额名单
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/limit/importFile")
    @ResponseBody
    public AjaxResult importFile(@RequestParam("file") MultipartFile file) throws Exception {
        List<Object> errorMsgList = new ArrayList<>();
        String name = file.getOriginalFilename();
        if (name.length() < 6 || !name.substring(name.length() - 5).equals(".xlsx")) {
            errorMsgList.add("文件格式错误,请上传xlsx格式");
            return AjaxResult.success(errorMsgList);
        }
        //解析文件
        ExcelUtil<IncreasedLimitList> util = new ExcelUtil<IncreasedLimitList>(IncreasedLimitList.class);
        List<IncreasedLimitList> list = util.importExcel(file.getInputStream());
        if (!ObjectUtil.isNotEmpty(list)) {
            errorMsgList.add("提额名单是空文件");
            return AjaxResult.success(errorMsgList);
        }
        errorMsgList.add(creditAccountInfoService.importFile(list));
        return AjaxResult.success(errorMsgList);
    }

}

